$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'forYoung/foryouny/list',
        datatype: "json",
        colModel: [
			{ label: '标题', name: 'title', index: 'title', width: 80 },
			{ label: '头像', name: 'headImg', index: 'head_img', width: 80, formatter: function(value, options, row){
                    if(value!=null)
                        return   `<img src="${value}" style="height:80px;width: 80px "   height:"40px" />`;
                    else
                        return ``;
                }  },
			{ label: '昵称', name: 'nackName', index: 'nack_name', width: 80 },
			{ label: '发布人', name: 'userId', index: 'user_id', width: 80 },
			{ label: '图片', name: 'imgs', index: 'imgs', width: 160, formatter: function(value, options, row) {

                    var dataImg = value.split(";")
                    var html = "";//${html}<a href="${dataImg[i]}" target="_blank"></a>
                    for (var i = 0;i < dataImg.length-1;i++){
                        html = `${html}<a href="${dataImg[i]}" target="_blank"><img src="${dataImg[i]}" style="float:left;width:40px;height:40px;" /></a>`
                    }
                    html=`<div">${html}</div>`
                    return html;

                }},
			{ label: '位置', name: 'address', index: 'address', width: 80 },
			{ label: '类型', name: 'type', index: 'type', width: 80 },
			{ label: '点赞数量', name: 'likeCount', index: 'like_count', width: 80 }, 			
			{ label: '评论数', name: 'commentCount', index: 'comment_count', width: 80 }, 			
			{ label: '分享次数', name: 'shareCount', index: 'share_count', width: 80 }, 			
			{ label: '收藏次数', name: 'collectionCount', index: 'collection_count', width: 80 },
			{ label: '发布时间', name: 'createTime', index: 'create_time', width: 80 },
            { label: '操作', name: 'isDel', index: '', width: 80 , formatter: function(value, options, row){
                    var html;
                    if(value==0){
                        html=` <button class="btn btn-danger" onclick="del(${row.id})"></i>&nbsp;删除</button>`
                    }else {
                        html=` <label class="label label-warning"></i>&nbsp;已删除</label>`
                    }
                    return html;
                }  },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

function del(id) {
	var user={
		id:id,
		isDel:2
	}

    confirm('确定要删除选中的记录？', function(){
        $.ajax({
            type: "POST",
            url: baseURL + "forYoung/foryouny/del",
            contentType: "application/json",
            data: JSON.stringify(user),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        $("#jqGrid").trigger("reloadGrid");
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		forYouny: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.forYouny = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.forYouny.id == null ? "forYoung/foryouny/save" : "forYoung/foryouny/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.forYouny),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "forYoung/foryouny/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "forYoung/foryouny/info/"+id, function(r){
                vm.forYouny = r.forYouny;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});